<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-left">
        <div class="login-left-content">
          <div class="brand-logo">
            <svg width="64" height="64" viewBox="0 0 64 64" fill="none" xmlns="http://www.w3.org/2000/svg">
              <rect width="64" height="64" rx="14" fill="#4caf50" />
              <path d="M16 32 L32 16 L48 32" stroke="#ffffff" stroke-width="4" fill="none"/>
              <path d="M24 48 L32 40 L40 48" stroke="#81c784" stroke-width="4" fill="none"/>
              <circle cx="32" cy="32" r="8" fill="#81c784"/>
            </svg>
          </div>
          <h1 class="welcome-title">欢迎回来</h1>
          <p class="welcome-subtitle">农产品金融销售平台</p>
          
          <div class="platform-benefits">
            <div class="benefit-item">
              <div class="benefit-icon">
                <i class="el-icon-shopping-cart-full"></i>
              </div>
              <div class="benefit-text">
                <h3>便捷购物</h3>
                <p>一站式农产品购物体验</p>
              </div>
            </div>
            <div class="benefit-item">
              <div class="benefit-icon">
                <i class="el-icon-money"></i>
              </div>
              <div class="benefit-text">
                <h3>金融支持</h3>
                <p>灵活的农业金融解决方案</p>
              </div>
            </div>
            <div class="benefit-item">
              <div class="benefit-icon">
                <i class="el-icon-user"></i>
              </div>
              <div class="benefit-text">
                <h3>专家咨询</h3>
                <p>农业专家在线咨询服务</p>
              </div>
            </div>
          </div>
          
          <!-- <div class="decoration-circles">
            <div class="circle circle-1"></div>
            <div class="circle circle-2"></div>
            <div class="circle circle-3"></div>
          </div> -->
        </div>
      </div>
      
      <div class="login-right">
        <div class="login-form-container">
          <div class="form-header">
            <h2>账号登录</h2>
            <p>登录您的账号，享受智慧农业服务</p>
          </div>
          
          <el-form ref="loginForm" :model="loginForm" :rules="rules" class="login-form">
            <el-form-item prop="username">
              <el-input 
                v-model="loginForm.username" 
                placeholder="请输入用户名" 
                prefix-icon="el-icon-user">
              </el-input>
            </el-form-item>

            <el-form-item prop="password">
              <el-input 
                type="password" 
                v-model="loginForm.password" 
                placeholder="请输入密码" 
                prefix-icon="el-icon-lock"
                show-password>
              </el-input>
            </el-form-item>

            <el-form-item>
              <div class="validate-container">
                <el-input 
                  v-model="loginForm.validCode" 
                  placeholder="验证码" 
                  prefix-icon="el-icon-key">
                </el-input>
                <ValidCode @input="createValidCode" class="validate-code" />
              </div>
            </el-form-item>

            <el-form-item>
              <el-button 
                type="success" 
                class="login-button" 
                @click="onLogin">
                登录
              </el-button>
            </el-form-item>

            <div class="login-actions">
              <el-link type="success" @click="$router.push('/forget')" class="forget-link">
                忘记密码？
              </el-link>
              <el-link type="success" @click="toRegister" class="register-link">
                立即注册
              </el-link>
            </div>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import ValidCode from "../components/Validate";
import request from "@/utils/request";
import { setRoutes } from "@/router";

export default {
  name: 'Login',
  components: {
    ValidCode
  },
  data() {
    return {
      validCode: '', //通过valicode获取的验证码
      loginForm: {
        username: '',
        password: '',
        validCode: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    toRegister() {
      this.$router.push("/register");
    },
    createValidCode(data) {
      this.validCode = data
    },
    onLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 验证码比较时转换为小写，实现不区分大小写
          if (this.loginForm.validCode.toLowerCase() !== this.validCode.toLowerCase()) {
            this.$message.error("验证码错误");
            return;
          }
          request.post("/user/login", this.loginForm)
            .then(res => {
              if (res.code === "0") {
                this.$message.success("登录成功");
                if (res.data.token) {
                  localStorage.setItem("token", res.data.token);
                }
                if (res.data) {
                  if (res.data.role === 'USER') {
                    localStorage.setItem("frontUser", JSON.stringify(res.data));
                  } else {
                    localStorage.setItem("backUser", JSON.stringify(res.data));
                  }
                }

                // 根据用户角色决定跳转路径
                if (res.data.role !== 'USER') {
                  if (res.data.menuList) {
                    localStorage.setItem("userMenuList", JSON.stringify(res.data.menuList));
                  }
                  setRoutes();
                  this.$router.push('/showView');
                } else {
                  this.$router.push('/');
                }
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch(error => {
              console.error("登录失败:", error);
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  background-color: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-wrapper {
  display: flex;
  width: 1000px;
  min-height: 600px;
  background: #ffffff;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.login-left {
  width: 45%;
  background: linear-gradient(135deg, #4caf50 0%, #81c784 100%);
  color: white;
  position: relative;
  overflow: hidden;
}

.login-left-content {
  padding: 50px 40px;
  position: relative;
  z-index: 2;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.brand-logo {
  margin-bottom: 30px;
}

.welcome-title {
  font-size: 36px;
  font-weight: 700;
  margin: 0 0 10px;
}

.welcome-subtitle {
  font-size: 18px;
  opacity: 0.9;
  margin-bottom: 50px;
}

.platform-benefits {
  margin-top: 30px;
}

.benefit-item {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
}

.benefit-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 15px;
}

.benefit-icon i {
  font-size: 20px;
}

.benefit-text h3 {
  font-size: 16px;
  margin: 0 0 5px;
  font-weight: 600;
}

.benefit-text p {
  font-size: 14px;
  margin: 0;
  opacity: 0.8;
}

.decoration-circles {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
  overflow: hidden;
}

.circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: -100px;
}

.circle-2 {
  width: 200px;
  height: 200px;
  bottom: -50px;
  right: -50px;
}

.circle-3 {
  width: 150px;
  height: 150px;
  bottom: 100px;
  left: -50px;
}

.login-right {
  width: 55%;
  padding: 40px;
  display: flex;
  align-items: center;
}

.login-form-container {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
}

.form-header {
  margin-bottom: 30px;
  text-align: center;
}

.form-header h2 {
  font-size: 28px;
  color: #333;
  margin: 0 0 10px;
}

.form-header p {
  font-size: 16px;
  color: #666;
  margin: 0;
}

.login-form {
  margin-top: 20px;
}

.validate-container {
  display: flex;
  gap: 16px;
}

.validate-code {
  flex-shrink: 0;
  width: 120px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #dcdfe6;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 600;
  border-radius: 8px;
  margin-top: 10px;
}

.login-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

:deep(.el-input__inner) {
  height: 44px;
  line-height: 44px;
  border-radius: 8px;
  transition: all 0.3s ease;
}

:deep(.el-input__inner:focus) {
  border-color: #4caf50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
}

:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-input__prefix) {
  left: 12px;
}

:deep(.el-input__inner) {
  padding-left: 38px;
}

:deep(.el-button--success) {
  background-color: #4caf50;
  border-color: #4caf50;
}

:deep(.el-button--success:hover) {
  background-color: #2e7d32;
  border-color: #2e7d32;
  transform: translateY(-2px);
  box-shadow: 0 8px 15px rgba(76, 175, 80, 0.3);
}

:deep(.el-link--success) {
  color: #4caf50;
}

:deep(.el-link--success:hover) {
  color: #2e7d32;
}

@media (max-width: 768px) {
  .login-wrapper {
    flex-direction: column;
    width: 100%;
  }
  
  .login-left {
    width: 100%;
    padding: 30px 20px;
  }
  
  .login-right {
    width: 100%;
    padding: 30px 20px;
  }
  
  .welcome-title {
    font-size: 28px;
  }
  
  .welcome-subtitle {
    font-size: 16px;
    margin-bottom: 30px;
  }
  
  .platform-benefits {
    display: none;
  }
}
</style>